<script setup lang="ts" name="Goods">
import useStore from '@/store'
import { storeToRefs } from 'pinia'
import { onUnmounted, watch, watchEffect } from 'vue'
import { useRoute } from 'vue-router'

const { goods } = useStore()
const { getGoodsInfo } = goods
const { info } = storeToRefs(goods)
const route = useRoute()

// 最好在 watchEffect 里面通过监听路由地址的变化发请求，为了路由缓存问题
/* watch(
  () => route.params.id,
  (newValue) => {
    if (route.fullPath === `/goods/${newValue}`) {
      getGoodsInfo(newValue as string)
    }
  },
  {
    immediate: true,
  }
) */

// 和上面等价
const unWatch = watchEffect(() => {
  const goodsId = route.params.id
  if (route.fullPath === `/goods/${goodsId}`) {
    getGoodsInfo(goodsId as string)
  }
})
onUnmounted(() => {
  unWatch()
})
</script>

<template>
  <div class="xtx-goods-page">
    <!-- 最好外面加 v-if，一旦条件成立，表示后端数据肯定过来了 -->
    <div class="container" v-if="info.categories">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">{{ info.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ info.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ info.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info"></div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs"></div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
